package cn.jinjimi.app.custom.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import cn.jinjimi.base.util.URLUtil;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.Insurance;
import cn.jinjimi.app.custom.entity.RemindBean;
import cn.jinjimi.app.custom.iview.CustomRemindView;
import cn.jinjimi.app.custom.ui.activity.CardListActivity;
import cn.jinjimi.app.entity.Holiday;
import cn.jinjimi.app.presenter.RemindPresenter;
import cn.jinjimi.app.main.activity.WebActivity;
import cn.jinjimi.app.main.adapter.CustomRemindAdapter;
import cn.jinjimi.app.main.extend.BaseFragment;
import cn.jinjimi.app.main.widget.XListView.XListView;
import cn.jinjimi.app.util.PreventContinuousClickUtil;

import java.util.Collections;
import java.util.List;

/**
 * 客户-提醒
 * <p/>
 * Created by jinsen on 15/10/21.
 */
public class CustomRemindFragment extends BaseFragment implements CustomRemindView
        , XListView.IXListViewListener, AdapterView.OnItemClickListener {

    RemindPresenter mPresenter;

    RelativeLayout mRootView;
    XListView mListView;
    View mGuideView;

    View mLoadingView;

    CustomRemindAdapter mAdapter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_remind_layout;
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        mRootView = (RelativeLayout) findView().findViewById(R.id.root_view);
        mListView = (XListView) findView().findViewById(R.id.remind_listview);
        mGuideView = findView().findViewById(R.id.remind_guideview);
        mGuideView.setVisibility(View.VISIBLE);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(true);
        mListView.setTransparentBg();
    }

    protected void initListener() {
        mListView.setOnItemClickListener(this);
        mListView.setXListViewListener(this);
    }

    protected void initData() {

        mAdapter = new CustomRemindAdapter(getActivity());
        mListView.setAdapter(mAdapter);
        mPresenter = new RemindPresenter(this);

        if (AccountManager.getInstance().isLogin()) {
            mPresenter.requestData();
        }
    }

    @Override
    public void setData(List<RemindBean> list) {
        if (list == null || list.isEmpty()) {
            mGuideView.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.VISIBLE);
            mAdapter.setItems(Collections.EMPTY_LIST);
        } else {
            mListView.setVisibility(View.VISIBLE);
            mGuideView.setVisibility(View.GONE);
            mAdapter.setItems(list);
        }
    }

    @Override
    public void loadSucc() {
        mRootView.removeView(mLoadingView);
        mLoadingView = null;
        mListView.stopRefresh();
        mListView.setRefreshTime(System.currentTimeMillis());
    }


    @Override
    public void loading() {
        if (mLoadingView == null) {
            mLoadingView = View.inflate(getActivity(), R.layout.view_loading_state_layout, null);
        }
        mListView.setVisibility(View.GONE);
        mRootView.addView(mLoadingView, new ViewGroup.LayoutParams(-1, -1));
    }

    @Override
    public void loadFail() {
        loadSucc();
    }

    @Override
    public void showToast(int resId) {

    }

    @Override
    public void onRefresh() {
        mPresenter.refresh();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (PreventContinuousClickUtil.isContinuousClick())
            return;

        int index = position - mListView.getHeaderViewsCount();
        if (index > 0) {
            RemindBean bean = (RemindBean) mAdapter.getItem(index);
            switch (bean.type) {
                case RemindBean.TYPE_CUSTOMER:
                    Custom custom = (Custom) bean.data;
                    sendBirtyDayCard(custom);
                    break;

                case RemindBean.TYPE_HOLIDAY:

                    Holiday holidayBean = (Holiday) bean.data;
                    sendHolidayCard(holidayBean);
                    break;

                case RemindBean.TYPE_INSURE:

                    sendInsureRemind((Insurance) bean.data);
                    break;
            }
        }
    }

    @EventTrace(EventID.APP_CUSTOMER_TIPS_BIRTH)
    private void sendBirtyDayCard(Custom custom) {
        Intent ci = new Intent(getActivity(), CardListActivity.class);
        ci.putExtra("title", custom.name);
        ci.putExtra("loadType", CardListActivity.LOAD_BIRTHDAY);
        ci.putExtra("customId", String.valueOf(custom.customerId));
        ci.putExtra("enter", MakeCardFragment.ENTER.HOLIDAY.getEnter());
        getActivity().startActivity(ci);
    }

    /**
     * 加载续费提醒的H5页面
     */
    @EventTrace(EventID.APP_CUSTOMER_TIPS_REPAID)
    private void sendInsureRemind(Insurance insurance) {

        String url = insurance.getRenewal_url();
        if (!TextUtils.isEmpty(url)) {
            if (AccountManager.getInstance().isLogin()) {
                URLUtil.appendParam(url, "token", AccountManager.getInstance().getAccount().token);
            }
        } else {
            url = insurance.getRemind_url();
        }
        WebActivity.launch(getActivity(), 1, url, getString(R.string.renew_remind));
    }

    @EventTrace(EventID.APP_CUSTOMER_TIPS_FESTIVAL)
    private void sendHolidayCard(Holiday holidayBean) {
        Intent holiday = new Intent(getActivity(), CardListActivity.class);
        holiday.putExtra("title", holidayBean.getName());
        holiday.putExtra("cardType", holidayBean.getHoliday_id());
        holiday.putExtra("loadType", CardListActivity.LOAD_HOLIDAY_WITH_ID);
        holiday.putExtra("enter", MakeCardFragment.ENTER.HOLIDAY.getEnter());
        getActivity().startActivity(holiday);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
            mPresenter = null;
        }
    }
}
